﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace KetBanBonPhuong
{
    /// <summary>
    /// Summary description for Status
    /// </summary>
    public class Status
    {
        private int _StatusID;

        public int StatusID
        {
            get
            {
                return _StatusID;
            }
            set
            {
                _StatusID = value;
            }
        }
        private int _UserID;

        public int UserID
        {
            get
            {
                return _UserID;
            }
            set
            {
                _UserID = value;
            }
        }
        private String _Content;

        public String Content
        {
            get
            {
                return _Content;
            }
            set
            {
                _Content = value;
            }
        }
        private DateTime _CreateDate;

        public DateTime CreateDate
        {
            get
            {
                return _CreateDate;
            }
            set
            {
                _CreateDate = value;
            }
        }


        public Status()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Status(SqlDataReader SqlReader)
        {
            if (SqlReader != null && !SqlReader.IsClosed)
            {
                _StatusID = SqlReader.GetInt32(0);
                if (!SqlReader.IsDBNull(1))
                {
                    _UserID = SqlReader.GetInt32(1);
                }
                if (!SqlReader.IsDBNull(2))
                {
                    _Content = SqlReader.GetString(2);
                }
                if (!SqlReader.IsDBNull(3))
                {
                    _CreateDate = SqlReader.GetDateTime(3);
                }
            }
        }

        public static DataTable GetAll()
        {
            DBHelp Dbh = new DBHelp();
            DataTable dt = new DataTable();
            String cmd = "Status_GetAll";
            dt = Dbh.ExecuteDataTable(cmd, CommandType.StoredProcedure);
            return dt;
        }

        public static Status GetInfo(int StatusID)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@status_id", StatusID);
            Status status = new Status();
            string cmd = "Status_GetInfo";
            SqlDataReader SqlReader = Dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            if (SqlReader.Read())
            {
                status.StatusID = SqlReader.GetInt32(0);
                if (!SqlReader.IsDBNull(1))
                {
                    status.UserID = SqlReader.GetInt32(1);
                }
                if (!SqlReader.IsDBNull(2))
                {
                    status.Content = SqlReader.GetString(2);
                }
                if (!SqlReader.IsDBNull(3))
                {
                    status.CreateDate = SqlReader.GetDateTime(3);
                }
            }
            return status;
        }

        public static DataTable GetAllStatus(int UserID)
        {
            DBHelp dbh = new DBHelp();
            DataTable dt = new DataTable();
            dbh.AddParameter("@user_id", UserID);
            String cmd = "select * from tbl_status where user_id = @user_id";
            dt = dbh.ExecuteDataTable(cmd, CommandType.Text);
            return dt;
        }

        public static int Insert(Status status)
        {
            DBHelp Dbh = new DBHelp();
            SqlParameter StatusID = new SqlParameter();
            StatusID.DbType = DbType.Int32;
            StatusID.Direction = ParameterDirection.Output;
            StatusID.ParameterName = @"@status_id";
            Dbh.AddParameter(StatusID);


            Dbh.AddParameter("@user_id", status.UserID);
            Dbh.AddParameter("@text", status.Content);
            Dbh.AddParameter("@created_date", status.CreateDate);

            try
            {
                string cmd = "Status_Insert";
                Dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
                return (int)StatusID.Value;
            }
            catch (System.Exception)
            {
                return -1;
            }

        }

        public static void Update(Status status)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@status_id", status.StatusID);
            Dbh.AddParameter("@user_id", status.UserID);
            Dbh.AddParameter("@text", status.Content);
            Dbh.AddParameter("@created_date", status.CreateDate);

            string cmd = "Status_Update";
            Dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        public static void Delete(int StatusID)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@status_id", StatusID);
            string cmd = "Status_Delete";
            Dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

    }
}